<?php
//      MTSM - Minetest Server Manager
//      (C) Chris Dorman, 2021
//      License: CC-BY-SA version 3.0
//      http://github.com/Pentium44/MTSM

include("config.php");

function recurse_copy($src,$dst) { 
    $dir = opendir($src); 
    @mkdir($dst); 
    while(false !== ( $file = readdir($dir)) ) { 
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($src . '/' . $file) ) { 
                recurse_copy($src . '/' . $file,$dst . '/' . $file); 
            } 
            else { 
                copy($src . '/' . $file,$dst . '/' . $file); 
            } 
        } 
    } 
    closedir($dir); 
}

function generateToken() {
    $length = 12;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomToken = '';
    for ($i = 0; $i < $length; $i++) {
        $randomToken .= $characters[rand(0, $charactersLength - 1)];
    }
    
    file_put_contents("mtm_db/token.php", "<?php \$reg_token = \"$randomToken\"; ?>");
}

function loginForm() {
?>
        <br />
        <div class="login">
		Don't have an account? No problem, <a href="<?php echo $_SERVER['PHP_SELF']; ?>?forms=register">register here</a>!<br /><br />
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=login" method="post">
			<table><tr><td>
                        Username:</td><td> <input class="text" type="text" name="username"></td></tr><tr><td>
                        Password:</td><td> <input class="text" type="password" name="password"></td></tr></table>
                        <input style="padding: 2px;" class="text" type="submit" name="submitBtn" value="Login">
                </form>
        </div>
<?php
}

function changePassForm() {
?>
        <br />
        <div class="chgpass">
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=changepass" method="post">
			<table><tr><td>
                        Old password:</td><td> <input class="text" type="password" name="oldpass"></td></tr><tr><td>
                        Password:</td><td> <input class="text" type="password" name="password"></td></tr><tr><td>
			Password Again:</td><td> <input class="text" type="password" name="password_again"></td></tr>
			</table>
                        <input class="text" type="submit" name="submitBtn" value="Change">
                </form>
        </div>
<?php
}

/*function uploadForm() {

       	print <<<EOD
			Upload
			<table style="margin:auto;">
				
				<form action="upload.php" method="post" enctype="multipart/form-data">
				<tr>
					<td>
					<input type="file" name="file[]" id="file" multiple><br>
					</td>
					<td>
					<input type="submit" name="submit" value="Upload">
					</td>
				</tr>
				</form>
				
				</table>
EOD;

}*/

function modUploadForm() {
?>
    <p>
    Warning: Mod uploads MUST be in ZIP archive format and MUST have the mod contents WITHIN a parent directory 
    within the archive. Failure to do so would cause for non-working mod installations into your user mods menu.
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=upload-modzip" method="POST" enctype="multipart/form-data">
         <input type="file" name="file"></input>
         <input style="padding: 2px;" class="text" type="submit" name="upload-btn" value="Upload">
    </form>
    </p>
<?php
}

function modsForm() {
        ?>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=configure-mods" method="post">
            <table><tr>
        <?php
    chdir("mods");
    $docount = 0;
    foreach(glob("*") as $filename) {
        echo "<td><input class='text' type='checkbox' name='$filename'> $filename</td>\n";
        $docount++;
        if($docount == 3) {
            echo "</tr><tr>";
            $docount = 0;
        }
    }
    chdir("..");
        ?>
            </tr></table>
            <input style="padding: 2px;" class="text" type="submit" name="submitBtn" value="Set mods">
            </form>
        <?php
}

function userModsForm($username) {

    if(count(glob("mtm_db/usermods/$username/*")) !== 0) {
        ?>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=configure-user-mods" method="post">
        <?php
        chdir("mtm_db/usermods/$username/");
        foreach(glob("*") as $filename) {
            echo "<input class='text' type='checkbox' name='$filename'> $filename<br />\n";
        }
        chdir("../../../");
        ?>
            <input style="padding: 2px;" class="text" type="submit" name="submitBtn" value="Set mods">
            </form>
        <?php
    } else {
        echo "No user mods available!";
    }
}

function settingsForm() {
   ?>
        <div class="login">
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=configure-server" method="post">
                <input class="text" type="checkbox" name="enablepvp"> Enable PVP<br />
                <input class="text" type="checkbox" name="creative"> Creative Mode<br />
                <input class="text" type="checkbox" name="enabledamage"> Enable Damage<br />
                <input class="text" type="checkbox" name="serverannounce"> Server Announcement<br />
                Server Name: <input class="text" type="text" name="servername"><br />
                Server Desc: <input class="text" type="text" name="serverdesc"><br />
                Server Website: <input class="text" type="text" name="serverurl"><br />
                Server MOTD: <input class="text" type="text" name="motd"><br /> 
                Map Generation: <select name="mgname">
                    <option value="v7">v7</option>
                    <option value="v6">v6</option>
                    <option value="v5">v5</option>
                    <option value="valleys">valleys</option>
                    <option value="carpathian">carpathian</option>
                    <option value="flat">flat</option>
                    <option value="fractal">fractal</option>
                </select><br />
                Default Privs: <input class="text" type="text" name="defaultprivs" value="shout, interact, home"><br />
                Spawnpoint (Format example: '32, 20, -140')<br /> <input class="text" type="text" name="staticspawnpoint"><br />
                Player Transfer Distance (Value in chunks, 0 = unlimited)<br />
                <input class="text" type="text" style="width: 50px;" name="playertransferdistance"><br />
                <input class="text" type="submit" name="submitBtn" value="Set">
                
                </form>
        </div>

    <?php
}

function registerForm() {
?>
        <br />
        <div class="login">
		Don't have a registration code? Reach out! cddo [at] riseup [dot] net<br />
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=register" method="post">
		<table><tr><td>
			Username:</td><td> <input class="text" type="text" name="username"></td></tr><tr><td>
			Full name:</td><td> <input class="text" type="text" name="fullname"></td></tr><tr><td>
			Email:</td><td><input class="text" type="text" name="email"></td></tr><tr><td>
			Password:</td><td> <input class="text" type="password" name="password"></td></tr><tr><td>
			Password again:</td><td> <input class="text" type="password" name="password-again"></td></tr><tr><td>
<?php if($GLOBALS['reg_token_required'] == "true") { echo 'Registration token (required):</td><td> <input class="text" type="text" name="codeword"></td></tr><tr><td>'; } ?>
            Go:</td><td>            <input class="text" type="submit" name="submitBtn" value="Register">
			</td></tr></table>
                </form>
        </div>
<?php
}
?>
